import { defineStore } from 'pinia'
import { STORAGE_KEYS } from '@/utils/constants'

// 定义应用状态管理
const useAppStore = defineStore('app', {
  state: () => ({
    // 应用主题
    theme: localStorage.getItem(STORAGE_KEYS.THEME) || 'light',
    // 当前语言
    language: localStorage.getItem(STORAGE_KEYS.LANGUAGE) || 'zh-CN',
    // 侧边栏状态
    sidebar: {
      opened: true,
      withoutAnimation: false
    },
    // 设备类型
    device: 'desktop',
    // 页面加载状态
    loading: false,
    // 页面标题
    title: '',
    // 面包屑导航
    breadcrumbs: [],
    // 全屏状态
    fullscreen: false
  }),

  getters: {
    // 获取侧边栏状态
    getSidebarOpened: (state) => state.sidebar.opened,
    // 获取设备类型
    getDevice: (state) => state.device,
    // 获取主题
    getTheme: (state) => state.theme,
    // 获取语言
    getLanguage: (state) => state.language,
    // 获取加载状态
    getLoading: (state) => state.loading,
    // 获取页面标题
    getTitle: (state) => state.title,
    // 获取面包屑导航
    getBreadcrumbs: (state) => state.breadcrumbs,
    // 获取全屏状态
    getFullscreen: (state) => state.fullscreen
  },

  actions: {
    // 切换侧边栏
    toggleSidebar(withoutAnimation = false) {
      this.sidebar.opened = !this.sidebar.opened
      this.sidebar.withoutAnimation = withoutAnimation
    },

    // 关闭侧边栏
    closeSidebar(withoutAnimation = false) {
      this.sidebar.opened = false
      this.sidebar.withoutAnimation = withoutAnimation
    },

    // 打开侧边栏
    openSidebar(withoutAnimation = false) {
      this.sidebar.opened = true
      this.sidebar.withoutAnimation = withoutAnimation
    },

    // 设置设备类型
    setDevice(device) {
      this.device = device
    },

    // 设置主题
    setTheme(theme) {
      this.theme = theme
      localStorage.setItem(STORAGE_KEYS.THEME, theme)
    },

    // 设置语言
    setLanguage(language) {
      this.language = language
      localStorage.setItem(STORAGE_KEYS.LANGUAGE, language)
    },

    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },

    // 设置页面标题
    setTitle(title) {
      this.title = title
    },

    // 设置面包屑导航
    setBreadcrumbs(breadcrumbs) {
      this.breadcrumbs = breadcrumbs
    },

    // 切换全屏
    toggleFullscreen() {
      this.fullscreen = !this.fullscreen
      if (this.fullscreen) {
        document.documentElement.requestFullscreen()
      } else {
        document.exitFullscreen()
      }
    }
  }
})

export default useAppStore